<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once( APPPATH . 'libraries/facebook/autoload.php' );

// Load the required PHP classes from the Facebook SDK.
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;

class admin_news extends CI_Controller {
	protected $_page_name = "admin_news";
	protected $_page_id = 'ul_postcontainer';
	protected $data; //data for this page.
	protected $submitdata; //data submit.
	protected $_page_title = ADMINTITLE_NEWS;
	protected $_base_url;
	protected $upload_control_name = "image1";
	protected $uploadfile_control_name = "file1";
    protected $NEWS = "news@";

    function  __construct()  {
		$this->_initdata();
	}
	
	//Controller: default
	function index() {
		//1. show new list
		$this->index_get_news_list();
		//2. get danh muc tin (news_cate)
		$this->data['aNewsCate'] = $this->ddl_get_news_cate_list();
		$this->data['aNewsCate_DataGrid'] = $this->dg_get_news_cate_list();
		//3. reset message
		if (isset($_SESSION['msg']))
			$this->data['msg'] = $_SESSION['msg'];
		$_SESSION['msg'] = "";
		//4. show to browser.
		$this->showadmin($this->data);
	}		
	
  //Controller: search
	function search()
	{
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			$iCurPage=1;
			$iCurPage = $_POST['h_cur_page'];
			$iRow_Count=0;
			@$limit=PAGESIZE;
			$start=$iCurPage*PAGESIZE-PAGESIZE;
			if(!($start > 0)) { 
			$start = 0;
			}
			$eu = ($start - 0); 
			if(!$limit > 0 ){ 
			$limit = 20; 
			}                             
			list($iRow_Count, $this->data['aNews']) = $this->Adminmodel->_search_news_list($_POST['txt_title'], $_POST['cbo_news_cate'], $_POST['txt_create_date'], $eu, $limit);	
			$this->data['paging'] = $this->paging_search($iCurPage,ceil($iRow_Count/PAGESIZE),$this->config->item('base_url').$this->_page_name,$iRow_Count);
			$this->data['cur_page'] = 'search';		
			$this->data['txt_title'] = $_POST['txt_title'];
			$this->data['cbo_news_cate'] = $_POST['cbo_news_cate'];			
			$this->data['txt_create_date'] = $_POST['txt_create_date'];		
			$this->data['search'] = 1;	
			$this->data['previous_id'] = 1;			
			$this->data['img_url'] = 1;		
			$this->data['page_title']="Tin Tức";	
			$this->data['msg']="";
			//get danh muc tin (news_cate)
			$this->data['aNewsCate'] = $this->ddl_get_news_cate_list();	
			$this->data['aNewsCate_DataGrid'] = $this->dg_get_news_cate_list();					
			$this->showadmin($this->data);
		}
	}
	
	//Controller: edit
	function news_editimages()
	{
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			$id =  $_POST['h_id'];
			if ($id != "")
			{
				$org_file = $_POST['h_img_url'];	
				//Hình thumb 1: execute crop & thumb:
				$file1 = $this->Adminmodel->_crop_image($org_file, $_POST['x1'],$_POST['y1'], $_POST['w1'], $_POST['h1'],1);
				$data_update = array();	
				$data_update["img_thumb"] = $file1;
				//Hình thumb 2: crop xong --> thumb. (auto-delete hình crop)
				$file2 = $this->Adminmodel->_crop_image($org_file, $_POST['x2'],$_POST['y2'], $_POST['w2'], $_POST['h2'],2);
				$upload_path = "news/";
				$ext = $this->Adminmodel->findexts($org_file) ;
				$a = explode("/", $file2);
				$filename = $a[count($a)-1];	
				if ($ext == "gif")
					$file_type = "image/gif";
				elseif ($ext == "jpg")
					$file_type = "image/jpeg";
				elseif ($ext == "png")
					$file_type = "image/pjpeg";					
				$filename_thumb_db = $this->Adminmodel->save_image_thumb($id, IMAGE_ROOT.$file2, $filename, $file_type, $upload_path, IMAGE_X_SIZE_1, IMAGE_Y_SIZE_1, "2");
				$data_update["img_thumb2"] = $filename_thumb_db;
				//Hình thumb 3: crop xong --> thumb. (auto-delete hình crop)
				$file3 = $this->Adminmodel->_crop_image($org_file, $_POST['x3'],$_POST['y3'], $_POST['w3'], $_POST['h3'], 3);
				$upload_path = "news/";
				$ext = $this->Adminmodel->findexts($org_file) ;
				$a = explode("/", $file3);
				$filename = $a[count($a)-1];	
				if ($ext == "gif")
					$file_type = "image/gif";
				elseif ($ext == "jpg")
					$file_type = "image/jpeg";
				elseif ($ext == "png")
					$file_type = "image/pjpeg";					
				$filename_thumb_db = $this->Adminmodel->save_image_thumb($id, IMAGE_ROOT.$file3, $filename, $file_type, $upload_path, IMAGE_X_SIZE_2, IMAGE_Y_SIZE_2, "3");
				$data_update["img_thumb3"] = $filename_thumb_db;				
				//save DB:
				$this->Adminmodel->_update_news('news', 'news_id', $id, $data_update);		
			}
			//thong bao & redirect page
			$_SESSION['msg'] = "Bạn đã cập nhật ".$this->_page_title." thành công.";
			$this->load->helper('url');
			echo "<script>window.location='".$this->data['base_url'].'admin_news/1'."'</script>";				
		}		
		else
		{
			$this->data["page_name"]="admin_news_editimages";
			$this->data['news_detail'] = $this->Adminmodel->_get_news_detail($this->uri->segment(3));	
			$this->data['aNewsCate'] = $this->ddl_get_news_cate_list();	
			$this->data['id'] = $this->uri->segment(3);
			$this->data['previous_id'] = 1;			
			$this->data['img_url'] = 1;		
			$this->data['page_title']="Tin Tức";			
			$this->load->view('common/common_view_admin',$this->data);					
		}
	}	
	
	//Controller: edit
	function edit()
	{
		$aNewsDetails = array();
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			$id =  $_POST['h_id'];
			if ($id != "")
			{

				//get submit data
				$this->submitdata['cate_id'] =  $_POST['cbo_news_cate'];
				$this->submitdata['parent_cate_url'] = $this->Adminmodel->_get_url_of_parent_cate($_POST['cbo_news_cate']); //get url của cate cha của cate news hiện hành.
				$this->submitdata['title'] =  $_POST['txt_title'];
				$this->submitdata['url'] =  convertToVn(trim($_POST['txt_title']));
				$this->submitdata['short_desc'] =  $_POST['txt_short_desc'];
				$str = $_POST['txt_content']; 
				$this->submitdata['full_desc'] = $str;			
				$this->submitdata['online_date'] = $_POST['txt_display_date'];		
				$this->submitdata['display_date'] = $_POST['txt_display_date'];			
				$this->submitdata['tag'] = $_POST['txt_tag'];		
				$this->submitdata['Status'] =  $_POST['cbo_Status'];
				$this->submitdata['source'] =  $_POST['txt_source'];
				if (isset($_POST['txt_is_hot']) && $_POST['txt_is_hot'] == "on")
					$this->submitdata['is_hot'] = "1";
				else
					$this->submitdata['is_hot'] = "0";
				$this->submitdata['news_id'] =  $_POST['h_id'];
				//upload image
				if ($_FILES[$this->upload_control_name]['name'] != "")
				{
					//get news details.
					$aNewsDetails = $this->Adminmodel->_get_news_detail($id);
					//move to deleted folder.
					$this->delete_news_image($aNewsDetails[0]->img, $aNewsDetails[0]->img_thumb);		
					$upload_path = "news/";
					$data_update = array();			
					
					//upload product images
					$data_update=$this->upload_images($upload_path, $id, $data_update);
					
					//update database
					if (count($data_update)>0)
						$this->Adminmodel->_update_news('news', 'news_id', $id, $data_update);									
				}
				//upload file
				if ($_FILES[$this->uploadfile_control_name]['name'] != "")
				{
					//get news details.
					if (count($aNewsDetails)==0)
						$aNewsDetails = $this->Adminmodel->_get_news_detail($id);
					//move to deleted folder.
					$this->delete_news_file($aNewsDetails[0]->file);		
					$upload_path = "news/";
					$data_update = array();			
					
					//upload product images
					$data_update=$this->upload_files($upload_path, $id, $data_update);
					
					//update database
					if (count($data_update)>0)
						$this->Adminmodel->_update_news('news', 'news_id', $id, $data_update);									
				}			
				
				//update DB
				$this->Adminmodel->_update_news('news', "news_id", $_POST['h_id'], $this->submitdata);
                
				//write log	
				$this->Adminmodel->_write_log("Action,Step,Info","update news,update record,newsid=".$id."|IP=".$_SERVER['REMOTE_ADDR']."|user_be=".$this->Adminmodel->_get_session(SESSION_ADMINUSERNAME));				
			}
			//thong bao & redirect page
			$_SESSION['msg'] = "Bạn đã cập nhật ".$this->_page_title." thành công.";
			$this->load->helper('url');
			echo "<script>window.location='".$this->data['base_url'].'admin_news/1'."'</script>";				
		}		
		else
		{
			$this->data["page_name"]="admin_news_edit";
			$this->data['news_detail'] = $this->Adminmodel->_get_news_detail($this->uri->segment(3));	
			$this->data['aNewsCate'] = $this->ddl_get_news_cate_list();	
			$this->data['id'] = $this->uri->segment(3);
			$this->data['previous_id'] = 1;			
			$this->data['img_url'] = 1;		
			$this->data['page_title']="Tin Tức";			
			$this->load->view('common/common_view_admin',$this->data);					
		}
	}
	
	//Controller: add
	function add()
	{
		$this->data["page_name"]="admin_news_add";
		$this->data['previous_id'] = 1;
		$this->data['aNewsCate'] = $this->ddl_get_news_cate_list();		
		$this->load->view('common/common_view_admin',$this->data);
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			//get submit data
			$this->submitdata['cate_id'] =  $_POST['cbo_news_cate'];
			$this->submitdata['parent_cate_url'] = $this->Adminmodel->_get_url_of_parent_cate($_POST['cbo_news_cate']); //get url của cate cha của cate news hiện hành.
			$this->submitdata['title'] =  $_POST['txt_title'];
			$this->submitdata['url'] =  convertToVn(trim($_POST['txt_title']));
			$this->submitdata['short_desc'] =  $_POST['txt_short_desc'];
			$str = $_POST['txt_content']; 
			$this->submitdata['full_desc'] = $str;			
			$this->submitdata['create_user'] = $this->Adminmodel->_get_session(SESSION_ADMINUSERNAME);
			$this->submitdata['create_date'] = date("Y-m-d H:i:s");	
			$this->submitdata['tag'] = $_POST['txt_tag'];	
			if (isset($_POST['txt_is_hot']) && $_POST['txt_is_hot'] == "on")
				$this->submitdata['is_hot'] = "1";
			else
				$this->submitdata['is_hot'] = "0";
			$this->submitdata['online_date'] = $_POST['txt_display_date'];			
			$this->submitdata['display_date'] = $_POST['txt_display_date'];			
			$this->submitdata['Status'] =  $_POST['cbo_Status'];
			$this->submitdata['source'] =  $_POST['txt_source'];
			
                        //insert news
			$insert_id = $this->Adminmodel->_insert_news('news', $this->submitdata);	
			            
			$upload_path = "news/";
			$data_update = array();			
			
			//upload news images
			$data_update=$this->upload_images($upload_path, $insert_id, $data_update);
			
			//upload file (dành cho VBPQ)
			$data_update=$this->upload_files($upload_path, $insert_id, $data_update);			
			
			//update database
			if (count($data_update)>0)
				$this->Adminmodel->_update_news('news', 'news_id', $insert_id, $data_update);		

                        $fb_id = 0;
                        if (isset($_POST['chk_facebook']) && $_POST['chk_facebook'] == "on"){
                            try {

                                // Start the session
                                //session_start();

                                // Initialize the Facebook app using the application ID and secret.
                                FacebookSession::setDefaultApplication( '612646102106378','66eb5dcf12ae6fc05400611326ded468' );
                                FacebookSession::enableAppSecretProof(false);
                                $access_token = 'CAAItMslKAQoBACLdEYqesfWPzkajJepkIXmFLMiZBJIRW9ZBvV0zZBGjbzIooLSKA7neJmMtbCYMixUoqZAlnN8oAtfhxP4p6TF5ZBgihSsM72Lc9FTIYhLugacosxXlqFeckd7aGbzV3TM58Recophbrj8cwOkBWjb7DqV1UKexq0IQZBEeWLYQiGKOYUZBHU2CI6QU9NUP9n8w3kaIHN3CNbjibq7QB4ZD';
                                $session = new FacebookSession( $access_token );

                                $link = 'http://www.eximgate.vn/tin-xnk/' . $this->submitdata['url']. '-' . $insert_id;
                                
                                $response = (new FacebookRequest(
                                  $session, 'POST', '/790142474433481/feed', array(
                                        'link' => $link,
                                        'message' => $this->submitdata['title'],
                                        'access_token' => $access_token
                                  )
                                ))->execute()->getGraphObject();

                                echo "Posted with id: " . $response->getProperty('id');
                                $fb_id = $response->getProperty('id');
                                
                            } catch(FacebookRequestException $e) {
                                echo "Exception occured, code: " . $e->getCode();
                                echo " with message: " . $e->getMessage();
                            }   
                        }                        			
                        
			//write log	
			$this->Adminmodel->_write_log("Action,Step,Info", "insert news,insert DB,newsid=".$insert_id."|IP=".$_SERVER['REMOTE_ADDR']."|user_be=".$this->Adminmodel->_get_session(SESSION_ADMINUSERNAME));				
                        
			//thong bao & redirect page
			$_SESSION['msg'] = "Bạn đã thêm mới ".$this->_page_title." thành công.";
                        if (isset($_POST['chk_facebook']) && $_POST['chk_facebook'] == "on"){
                            $_SESSION['msg'] .= ' Posted with ID of Facebook: ' . $fb_id;
                        }
			$this->load->helper('url');
			echo "<script>window.location='".$this->data['base_url'].'admin_news/1'."'</script>";				
		}
	}
	
	//Controller: delete
	function delete()
	{
		$id=($this->uri->segment(3)!="")?$this->uri->segment(3):0;
		if ($id==0)
		{
			echo "fail";
		}
		else
		{
			//get news details.
			$aNewsDetails = $this->Adminmodel->_get_news_detail($id);
			//move to deleted folder.
			if ($aNewsDetails[0]->img != "" && $aNewsDetails[0]->img != "upimg_man/image/news/")
			{
				//echo "*** img=".$aNewsDetails[0]->img;
				$this->delete_news_image($aNewsDetails[0]->img, $aNewsDetails[0]->img_thumb);
			}
			if ($aNewsDetails[0]->{'file'} != "" && $aNewsDetails[0]->{'file'} != "upimg_man/image/news/")
			{
				//echo "*** file=".$aNewsDetails[0]->{'file'};exit;
				$this->delete_news_file($aNewsDetails[0]->{'file'});	
			}
			//write log.
			$this->Adminmodel->_write_log("Action,Step,Info", "delete news,delete record,newsid=".$id."|IP=".$_SERVER['REMOTE_ADDR']."|user_be=".$this->Adminmodel->_get_session(SESSION_ADMINUSERNAME));			
			echo $this->Adminmodel->_delete_news($id);
            
		}
	}		
	
	//local func : for combobox
	function ddl_get_news_cate_list()
	{
		$tmp1 = $this->Adminmodel->_get_news_cate_list();
		$tmp2 = $tmp1;
		$b = array();
		foreach($tmp1 as $key1=>$item1):
			if ($item1->parent_id=="")
			{
				$b[] = (object)array("parent_id"=>$item1->parent_id,
														 "parent_name"=>$item1->parent_name,
														 "cate_id"=>$item1->cate_id,
														 "cate_name"=>"&nbsp;&bull;&nbsp;".$item1->cate_name,
														 "url"=>$item1->url,
														 "status"=>$item1->status
														);		
				foreach($tmp2 as $key2=>$item2):
					if ($item1->cate_id == $item2->parent_id)
					{
						$b[] = (object)array("parent_id"=>$item2->parent_id,
																 "parent_name"=>$item2->parent_name,
																 "cate_id"=>$item2->cate_id,
																 "cate_name"=>"&nbsp;&bull;&nbsp;&bull;&nbsp;&bull;&nbsp;".$item2->cate_name,
																 "url"=>$item2->url,
																 "status"=>$item2->status
																);
					}
				endforeach; 
			}
		endforeach;
		return $b;
	}		
	
	//local func : for datagrid
	function dg_get_news_cate_list()
	{
		$tmp1 = $this->Adminmodel->_get_news_cate_list();
		$tmp2 = $tmp1;
		$b = array();
		foreach($tmp1 as $key1=>$item1):
			if ($item1->parent_id=="")
			{
				$b[$item1->cate_id] = $item1->cate_name;
				foreach($tmp2 as $key2=>$item2):
					if ($item1->cate_id == $item2->parent_id)
						$b[$item2->cate_id] = $item2->parent_name." &rArr; ".$item2->cate_name;
				endforeach; 
			}
		endforeach;
		return $b;
	}			
	
	//local func:
	function delete_news_image($img, $img_thumb)
	{
		$this->invoke_delete_file($img);
		$this->invoke_delete_file($img_thumb);		
	}
	
	//local func:
	function delete_news_file($file)
	{
		$this->invoke_delete_file($file);
	}
	
	//local func:
	function invoke_delete_file($file)
	{
		if ($file != "")
		{
			if (file_exists("./style/".$file))
			{
				$a = explode("/", $file);
				if (count($a) > 1)
				{
					if (copy("./style/".$file, IMAGE_ROOT.IMAGE_DIR."news/deleted/".$a[count($a)-1]))
						unlink("./style/".$file);
				}
			}
		}
	}	
	
	//local func:
	function index_get_news_list()
	{
		$iCurPage=1;
		$iCurPage = $this->uri->segment(2);
		$iRow_Count=0;
		@$limit=PAGESIZE;
		$start=$iCurPage*PAGESIZE-PAGESIZE;
		if(!($start > 0)) { 
		$start = 0;
		}
		$eu = ($start - 0); 
		if(!$limit > 0 ){ 
		$limit = 20; 
		}                             
		list($iRow_Count, $this->data['aNews']) = $this->Adminmodel->_get_news_list($eu, $limit);
		$this->data['paging'] = $this->paging($iCurPage,ceil($iRow_Count/PAGESIZE),$this->config->item('base_url').$this->_page_name,$iRow_Count);
		$this->data['cur_page'] = $iCurPage;
		$this->data['previous_id'] = 1;			
		$this->data['img_url'] = 1;			
	}
	
	//local func: init data
	function _initdata()
	{
		parent::__construct();
		session_start();
		$this->load->model('Adminmodel');
		$this->load->helper('url');
		$this->_check_admin_login();
		
		//init data (this page, header admin)
		$this->_base_url = $this->config->item('base_url');
		$this->data['base_url'] = $this->config->item('base_url');
		$this->data['page_title'] = $this->_page_title;
		$this->data['admintitle_news'] = ADMINTITLE_NEWS;
        $this->data['admintitle_news_auto'] = ADMINTITLE_NEWS_AUTO;
        $this->data['admintitle_video'] = ADMINTITLE_VIDEO;
        $this->data['admintitle_customs'] = ADMINTITLE_CUSTOMS;
		$this->data['admintitle_adv'] = ADMINTITLE_ADV;
		$this->data['admintitle_faq'] = ADMINTITLE_FAQ;
		$this->data['admintitle_contact'] = ADMINTITLE_CONTACT;
		$this->data['admintitle_common'] = ADMINTITLE_COMMON;
		$this->data['admintitle_stats'] = ADMINTITLE_STATS;
		$this->data['admintitle_beuser'] = ADMINTITLE_BEUSER;
		$this->data['website_name'] = WEBSITE_NAME;		
		$this->data['link_page_name'] = $this->_page_name;
		$this->data['is_addnew'] = "add"; //show link add / list
        $this->data['is_addlist'] = ""; //show link list
        $this->data['is_news_auto'] = "NOT"; //show button get news auto
		$this->data['page_id'] = $this->_page_id;	
		$this->data['page_name'] = $this->_page_name;
	}			
	
	//local func: check admin login
	function _check_admin_login()
	{
		if (($this->Adminmodel->_check_login(SESSION_ADMINUSERNAME) == false) ||
				(time() - $this->Adminmodel->_get_session(SESSION_ADMINTIMEOUT)) > SESSION_ADMINTIMEOUTVALUE
			 )
		{
			$base_url=$this->config->item('base_url');
			redirect($base_url.'admin_login', 'location', 301);
		}		
		else
			$this->Adminmodel->_set_session(SESSION_ADMINTIMEOUT,time());
	}	
	
	//local func
	function upload_images($upload_path, $insert_id, $data_update)
	{
            //upload image1: 1 thumbed images
            list($filename, $file_type, $filename1_db) = $this->save_image($insert_id, $upload_path, $this->upload_control_name);
            $data_update['img']=IMAGE_DIR.$upload_path.$filename;

            if ($filename != "")
            {
                $filename_thumb_db = $this->Adminmodel->save_image_thumb($insert_id, IMAGE_ROOT.IMAGE_DIR.$upload_path.$filename, $filename, $file_type, $upload_path, IMAGE_X_SIZE, IMAGE_Y_SIZE, "1");
                $data_update['img_thumb']=$filename_thumb_db;
                $filename_thumb_db = $this->Adminmodel->save_image_thumb($insert_id, IMAGE_ROOT.IMAGE_DIR.$upload_path.$filename, $filename, $file_type, $upload_path, IMAGE_X_SIZE_1, IMAGE_Y_SIZE_1, "2");
                $data_update['img_thumb2']=$filename_thumb_db;     
                $filename_thumb_db = $this->Adminmodel->save_image_thumb($insert_id, IMAGE_ROOT.IMAGE_DIR.$upload_path.$filename, $filename, $file_type, $upload_path, IMAGE_X_SIZE_2, IMAGE_Y_SIZE_2, "3");
                $data_update['img_thumb3']=$filename_thumb_db; 
            }

            return $data_update;
	}	
	
	//local func
	function save_image($insert_id, $upload_path, $upload_control_name)
	{
		return $this->Adminmodel->save_image($insert_id, $upload_path, $upload_control_name);
	}	
	
	//local func
	function upload_files($upload_path, $insert_id, $data_update)
	{
		//upload image1: 1 thumbed images
		list($filename, $file_type, $filename1_db) = $this->save_file($insert_id, $upload_path, $this->uploadfile_control_name);
		$data_update['file']=IMAGE_DIR.$upload_path.$filename;
		return $data_update;
	}	
	        
	//local func
	function save_file($insert_id, $upload_path, $upload_control_name)
	{
		return $this->Adminmodel->save_file($insert_id, $upload_path, $upload_control_name);
	}		
	
	//local func
	function paging($page, $totalpage, $pagelink, $totalrows)
	{
		$paging_str = '';
		$paging_str .= 'Trang: &nbsp;&nbsp;&nbsp;';
		if ($page > 1)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' ."'".$pagelink.'/'.($page-1)."'". '">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		
		// list 10 pagelink
		$startpage = 1;
		if ($page > 5)  $startpage = $page - 4;
		$endpage = $totalpage;
		if ($totalpage - $page > 5)  $endpage = $page + 5;
		for ($i=$startpage; $i<$page; $i++)
			$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link">' .$i. '</a>';
		$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link_selected">' .$page. '</a>';
		for ($i=$page+1; $i<=$endpage; $i++)
			$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link">' .$i. '</a>';
		
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		if ($page < $totalpage)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' ."'".$pagelink.'/'.($page+1)."'". '">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';
	
		$paging_str .= '&nbsp;&nbsp;&nbsp;<span class="paging_total"><strong>[ </strong>T&#7893;ng c&#7897;ng: <font color="Red">' .$totalrows. '</font> k&#7871;t qu&#7843; <strong> ]</strong></span>';
		return $paging_str;
	}	
	
	//local func: phân trang dùng cho function "Tìm Kiếm"
	function paging_search($page, $totalpage, $pagelink, $totalrows)
	{
		$paging_str = '';
		$paging_str .= 'Trang: &nbsp;&nbsp;&nbsp;';
		if ($page > 1)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle" style="cursor:pointer;" onclick="submitSearch(\''.$pagelink. '\','.($page-1).');return true;">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		
		// list 10 pagelink
		$startpage = 1;
		if ($page > 5)  $startpage = $page - 4;
		$endpage = $totalpage;
		if ($totalpage - $page > 5)  $endpage = $page + 5;
		for ($i=$startpage; $i<$page; $i++)
			$paging_str .= ' <a onclick="submitSearch(\''.$pagelink. '\','.$i.');return true;" class="link" href="#">'.$i.'</a>';
			//$paging_str .= ' <a onclick="submitSearch(\'"'.$pagelink. '></a>';;
		//$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link_selected">' .$page. '</a>';
		$paging_str .= ' <a onclick="submitSearch(\''.$pagelink. '\','.$i.');return true;" class="link_selected" href="#">'.$i.'</a>';
		for ($i=$page+1; $i<=$endpage; $i++)
			$paging_str .= ' <a onclick="submitSearch(\''.$pagelink. '\','.$i.');return true;" class="link" href="#">'.$i.'</a>';
			//$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link">' .$i. '</a>';
		
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		if ($page < $totalpage)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle" style="cursor:pointer;" onclick="submitSearch(\''.$pagelink. '\','.($page+1).');return true;">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';
	
		$paging_str .= '&nbsp;&nbsp;&nbsp;<span class="paging_total"><strong>[ </strong>T&#7893;ng c&#7897;ng: <font color="Red">' .$totalrows. '</font> k&#7871;t qu&#7843; <strong> ]</strong></span>';
		return $paging_str;
	}		
	
}
?>